package com.inspector.app.ui.chat.holder;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.inspector.app.BaseApp;
import com.inspector.app.R;
import com.inspector.app.ui.chat.adapter.ChatAdapter;
import com.inspector.app.ui.chat.entity.DataBean;
import com.inspector.app.ui.chat.entity.MsgSendStatus;
import com.inspector.app.ui.chat.entity.ReceiveMessagesEntity;
import com.inspector.app.ui.widget.CircleImageView;
import com.inspector.app.ui.chat.utils.DateUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class ItemImageSendHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
    private TextView itemTime;
    private TextView name;
    private CircleImageView itemHeader;
    private ImageView itemFail;
    private ImageView bivPic;
    private ProgressBar itemProgress;
    private ChatAdapter.OnItemListener mListener;
    private String image;
    private int position;
    private RelativeLayout itemLayout;
    private TextView messageRetraction;
    private ReceiveMessagesEntity mEntity;
    public ItemImageSendHolder(@NonNull View itemView) {
        super(itemView);
        itemTime = itemView.findViewById(R.id.item_tv_time);
        name = itemView.findViewById(R.id.name);
        itemHeader = itemView.findViewById(R.id.chat_item_header);
        itemFail = itemView.findViewById(R.id.chat_item_fail);
        bivPic = itemView.findViewById(R.id.bivPic);
        itemProgress = itemView.findViewById(R.id.chat_item_progress);
        itemLayout = itemView.findViewById(R.id.item_layout);
        messageRetraction = itemView.findViewById(R.id.message_retraction);
    }

    public void setOnItemListener(ChatAdapter.OnItemListener listener, int position) {
        this.mListener = listener;
        this.position = position;
    }

    public void bind(ReceiveMessagesEntity entity) {
        if (entity != null) {
            if (entity.gettShow() == 1) {
                itemLayout.setVisibility(GONE);
                itemTime.setVisibility(GONE);
                messageRetraction.setVisibility(VISIBLE);
            } else {
                mEntity = entity;
                itemLayout.setVisibility(VISIBLE);
                itemTime.setVisibility(VISIBLE);
                messageRetraction.setVisibility(GONE);
                itemTime.setVisibility(entity.getCreateTime() == 0 ? GONE : VISIBLE);

                String standardTimes = DateUtil.getStandardTimes(entity.getCreateTime());
                itemTime.setText(standardTimes);

                bivPic.setOnLongClickListener(this);
                name.setText(entity.getSendUserName());


                Glide.with(BaseApp.getInstance())
                        .load(entity.getSendHeadPic())
                        .apply(new RequestOptions().circleCrop())
                        .into(itemHeader);

                bivPic.setOnClickListener(this);

                MsgSendStatus sentStatus = entity.getSendStatus();
                if (sentStatus == MsgSendStatus.SENDING) {
                    itemProgress.setVisibility(VISIBLE);
                    itemFail.setVisibility(GONE);
                } else if (sentStatus == MsgSendStatus.FAILED) {
                    itemProgress.setVisibility(GONE);
                    itemFail.setVisibility(VISIBLE);
                } else if (sentStatus == MsgSendStatus.SENT) {
                    itemProgress.setVisibility(GONE);
                    itemFail.setVisibility(GONE);
                }else {
                    itemProgress.setVisibility(GONE);
                    itemFail.setVisibility(GONE);
                }
                String message = entity.getMessage();
                try {
                    JSONObject jsonObject = new JSONObject(message);
                    image = jsonObject.getString("content");
                    Glide.with(BaseApp.getInstance())
                            .load(image)
                            .into(bivPic);
                    itemFail.setOnClickListener(view -> {
                        if (mListener != null) {
                            mListener.OnItemResendListener(mEntity, image, position);
                        }
                    });
                } catch (JSONException e) {

                }


            }


        }

    }

    @Override
    public void onClick(View view) {
        if (mListener != null) {
            int[] location = new int[2];
            DataBean dataBean = new DataBean();
            if (bivPic != null) {
                bivPic.getLocationOnScreen(location);
                dataBean.url = image;
                dataBean.localX = location[0];
                dataBean.localY = location[1];
                dataBean.width = bivPic.getWidth();
                dataBean.height = bivPic.getHeight();
            }
            mListener.showImageListener(dataBean, position);
        }

    }

    @Override
    public boolean onLongClick(View view) {
        if (mListener != null) {
            mListener.OnItemLongListener(mEntity,  bivPic,position);
        }
        return true;
    }
}
